﻿package _7_Text;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.UndoableEdit;
/**
 * 2011-6-6 20:34:32
 * @author Administrator
 * 演示JTextArea的取消和重做功能，只要内存不溢出，可以无限次取消重做
 */
public class JTextArea_UndoRedo {
    JFrame frame;
    Container contentPane;
    JToolBar toolBar;
    JTextArea textArea;
    JButton undoButton,redoButton;
    Stack<UndoableEdit> undoStack,redoStack;
    public JTextArea_UndoRedo(){
        frame=new JFrame("JTextArea取消与重做测试");
        contentPane=frame.getContentPane();
        toolBar=new JToolBar();
        undoStack=new Stack<UndoableEdit>();
        redoStack=new Stack<UndoableEdit>();
        undoButton=new JButton("取消");
        undoButton.setEnabled(false);
        undoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UndoableEdit edit=undoStack.pop();
                edit.undo();
                redoStack.push(edit);
                if(!redoButton.isEnabled()){
                    redoButton.setEnabled(true);
                }
                if(undoStack.empty()){
                    undoButton.setEnabled(false);
                }
            }
        });
        redoButton=new JButton("重做");
        redoButton.setEnabled(false);
        redoButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UndoableEdit edit=redoStack.pop();
                edit.redo();
                undoStack.push(edit);
                if(!undoButton.isEnabled()){
                    undoButton.setEnabled(true);
                }
                if(redoStack.empty()){
                    redoButton.setEnabled(false);
                }
            }
        });
        toolBar.add(undoButton);
        toolBar.add(redoButton);
        contentPane.add(toolBar,BorderLayout.NORTH);
        textArea=new JTextArea(10,40);
        textArea.getDocument().addUndoableEditListener(new UndoableEditListener() {
            public void undoableEditHappened(UndoableEditEvent e) {
                if(!undoButton.isEnabled()){
                    undoButton.setEnabled(true);    
                }
                UndoableEdit edit=e.getEdit();
                undoStack.push(edit);
                System.out.println("UndoableEditEvent:"+edit.getPresentationName());
            }
        });
        contentPane.add(new JScrollPane(textArea),BorderLayout.CENTER);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
    public static void main(String args[]){
        new JTextArea_UndoRedo();
    }
}
